package top.xtcoder.jdcbase.base.knife4j;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import cn.dev33.satoken.stp.StpUtil;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * 正在开发中
 * @author xiangtao
 *
 */
//@Component
public class Knife4jPBImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar, BeanDefinitionRegistryPostProcessor, EnvironmentAware {
	private Knife4jPBProperties prop;
	
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

	}
	
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		
	}

	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Docket.class);
		GenericBeanDefinition gbdef = (GenericBeanDefinition) builder.getBeanDefinition();
		System.out.println("builder=" + gbdef.getBeanClassName());
		System.out.println("builder=" + gbdef.getPropertyValues().size());
			
//		for(int i = 0; i < prop.getApis().size(); i ++) {
//			GenericBeanDefinition def = (GenericBeanDefinition) gbdef;
//			def.setBeanClass(Docket.class);
//			def.getPropertyValues().add("groupName", "PB系统模块");
//			registry.registerBeanDefinition("defaultApi" + i, def);
//		}
	}

	@Override
	public void setEnvironment(Environment environment) {
		BindResult<Knife4jPBProperties> bres = Binder.get(environment).bind("knife4j", Knife4jPBProperties.class);
		prop = bres.get();
	}
	
	private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("职黔途平台API文档")
                .description("职黔途服务平台 RESTful APIs")
                .termsOfServiceUrl("http://www.jiudingcheng.com/")
                .contact(new Contact("呐喊,向涛","http://www.jiudingcheng.com/","jiudingcheng@163.com"))
                .version("1.0")
                .build();
    }

	//生成全局通用参数
	private List<Parameter> getGlobalRequestParameters() {
	    List<Parameter> parameters = new ArrayList<>();
	    parameters.add(new ParameterBuilder()
	            .name(StpUtil.getTokenName())
	            .description("接口校验参数")
	            .modelRef(new ModelRef("String"))
	            .required(false)
	            .parameterType("header")
	            .required(false)
	            .build());
	     return parameters;
    }
}
